home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / edge3.c < prev    next >
C/C++ Source or Header  |  1993-06-21  |  6KB  |  252 lines

  1.  
  2.  
  3.     /***********************************************
  4.     *
  5.     *    file d:\cips\edge3.c
  6.     *
  7.     *    Functions: This file contains
  8.     *       gaussian_edge
  9.     *       enhance_edges
  10.     *
  11.     *    Purpose:
  12.     *       These functions implement several
  13.     *       types of advanced edge detection.
  14.     *
  15.     *    External Calls:
  16.     *       wtiff.c - round_off_image_size
  17.     *                 create_file_if_needed
  18.     *                 write_array_into_tiff_image
  19.     *                 round_off_image_size
  20.     *       tiff.c - read_tiff_header
  21.     *       rtiff.c - read_tiff_image
  22.     *       numcvrt.c - get_integer
  23.     *       edge.c - fix_edges
  24.     *
  25.     *    Modifications:
  26.     *       26 March 1991 - created
  27.     *
  28.     ***********************************************/
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. #include "cips.h"
  36.  
  37.  
  38. short enhance_mask[3][3] =  {
  39.        {-1,  0, -1},
  40.        { 0,  4,  0},
  41.        {-1,  0, -1} };
  42.  
  43.  
  44.  
  45. short g7[7][7] = {
  46.      {  0,  0, -1, -1, -1,  0,  0},
  47.      {  0, -2, -3, -3, -3, -2,  0},
  48.      { -1, -3,  5,  5,  5, -3, -1},
  49.      { -1, -3,  5, 16,  5, -3, -1},
  50.      { -1, -3,  5,  5,  5, -3, -1},
  51.      {  0, -2, -3, -3, -3, -2,  0},
  52.      {  0,  0, -1, -1, -1,  0,  0}};
  53.  
  54. short g9[9][9] = {
  55.    {  0,  0,  0,  -1, -1, -1,  0,  0,  0},
  56.    {  0, -2, -3,  -3, -3, -3, -3, -2,  0},
  57.    {  0, -3, -2,  -1, -1, -1, -2, -3,  0},
  58.    { -1, -3, -1,   9,  9,  9, -1, -3, -1},
  59.    { -1, -3, -1,   9, 19,  9, -1, -3, -1},
  60.    { -1, -3, -1,   9,  9,  9, -1, -3, -1},
  61.    {  0, -3, -2,  -1, -1, -1, -2, -3,  0},
  62.    {  0, -2, -3,  -3, -3, -3, -3, -2,  0},
  63.    {  0,  0,  0,  -1, -1, -1,  0,  0,  0}};
  64.  
  65.  
  66.  
  67.    /************************************************
  68.    *
  69.    *   gaussian_edge(...
  70.    *
  71.    *
  72.    *************************************************/
  73.  
  74. gaussian_edge(in_name, out_name, the_image, out_image,
  75.               il, ie, ll, le, size, threshold, high)
  76.    char   in_name[], out_name[];
  77.    int    high, il, ie, ll, le, size, threshold;
  78.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  79. {
  80.    char response[80];
  81.    long sum;
  82.    int  a, b, absdiff, absmax, diff, i, j,
  83.         length, lower, max, new_hi, new_low,
  84.         scale, start, stop, upper, width;
  85.  
  86.    struct tiff_header_struct image_header;
  87.  
  88.  
  89.    create_file_if_needed(in_name, out_name, out_image);
  90.  
  91.    read_tiff_header(in_name, &image_header);
  92.  
  93.    new_hi  = 250;
  94.    new_low = 16;
  95.    if(image_header.bits_per_pixel == 4){
  96.        new_hi  = 10;
  97.        new_low = 3;
  98.    }
  99.  
  100.    max = 255;
  101.    if(image_header.bits_per_pixel == 4)
  102.       max = 16;
  103.  
  104.  
  105.    if(size == 7){
  106.       lower = -3;
  107.       upper =  4;
  108.       start =  3;
  109.       stop  =  ROWS-3;
  110.       scale =  2;
  111.    }
  112.  
  113.    if(size == 9){
  114.       lower = -4;
  115.       upper =  5;
  116.       start =  4;
  117.       stop  =  ROWS-4;
  118.       scale =  2;
  119.    }
  120.  
  121.  
  122.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  123.  
  124.    for(i=0; i<ROWS; i++)
  125.       for(j=0; j<COLS; j++)
  126.          out_image[i][j] = 0;
  127.  
  128.  
  129.    for(i=start; i<stop; i++){
  130.       if ( (i%10) == 0) printf(" i=%d", i);
  131.       for(j=start; j<stop; j++){
  132.  
  133.       sum = 0;
  134.  
  135.       for(a=lower; a<upper; a++){
  136.          for(b=lower; b<upper; b++){
  137.             if(size == 7)
  138.                sum = sum + the_image[i+a][j+b] *
  139.                      g7[a+3][b+3];
  140.             if(size == 9)
  141.                sum = sum + the_image[i+a][j+b] *
  142.                      g9[a+4][b+4];
  143.          } /* ends loop over a */
  144.       }  /* ends loop over b */
  145.  
  146.       if(sum < 0) sum = 0;
  147.       if(sum > max) sum = max;
  148.       out_image[i][j] = sum;
  149.  
  150.  
  151.       }  /* ends loop over j */
  152.    }  /* ends loop over i */
  153.  
  154.      /* if desired, threshold the output image */
  155.    if(threshold == 1){
  156.        for(i=0; i<ROWS; i++){
  157.           for(j=0; j<COLS; j++){
  158.              if(out_image[i][j] > high){
  159.                   out_image[i][j] = new_hi;
  160.              }
  161.              else{
  162.                   out_image[i][j] = new_low;
  163.              }
  164.           }
  165.        }
  166.    }  /* ends if threshold == 1 */
  167.  
  168.  
  169.    fix_edges(out_image, size/2);
  170.  
  171.    write_array_into_tiff_image(out_name, out_image,
  172.                                il, ie, ll, le);
  173.  
  174. } /* ends gaussian_edge */
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.      /*******************************************
  182.      *
  183.      * enhance_edges(...
  184.      *
  185.      * This function enhances the edges in an
  186.      * input image and writes the enhanced
  187.      * result to an output image.  It operates
  188.      * much the same way as detect_edges
  189.      * except it uses only one type of mask.
  190.      *
  191.      * The threshold and high parameters perform
  192.      * a different role in this function.  The
  193.      * threshold parameter does not exist.  The
  194.      * high parameter determines if the edge is
  195.      * strong enough to enhance or change the
  196.      * input image.
  197.      *
  198.      *******************************************/
  199.  
  200.  
  201. enhance_edges(in_name, out_name, the_image, out_image,
  202.              il, ie, ll, le, high)
  203.    char   in_name[], out_name[];
  204.    int    high, il, ie, ll, le;
  205.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  206.  
  207. {
  208.    int    a, b, i, j, k,
  209.           length, max, new_hi,
  210.           new_lo, sum, width;
  211.    struct tiff_header_struct image_header;
  212.  
  213.  
  214.    create_file_if_needed(in_name, out_name, out_image);
  215.  
  216.    read_tiff_header(in_name, &image_header);
  217.  
  218.    max = 255;
  219.    if(image_header.bits_per_pixel == 4)
  220.       max = 16;
  221.  
  222.  
  223.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  224.  
  225.          /* Do convolution over image array */
  226.    for(i=1; i<ROWS-1; i++){
  227.       if( (i%10) == 0) printf("%d ", i);
  228.       for(j=1; j<COLS-1; j++){
  229.          sum = 0;
  230.          for(a=-1; a<2; a++){
  231.             for(b=-1; b<2; b++){
  232.                sum = sum +
  233.                      the_image[i+a][j+b] *
  234.                      enhance_mask[a+1][b+1];
  235.             }
  236.          }
  237.          if(sum < 0)   sum = 0;
  238.          if(sum > max) sum = max;
  239.          if(sum > high)
  240.             out_image[i][j] = max;
  241.          else
  242.             out_image[i][j] = the_image[i][j];
  243.       }  /* ends loop over j */
  244.    }  /* ends loop over i */
  245.  
  246.    fix_edges(out_image, 1);
  247.  
  248.  
  249.    write_array_into_tiff_image(out_name, out_image,
  250.                                il, ie, ll, le);
  251. }  /* ends enhance_edges */
  252.